package com.mechat.ui.webview

import android.os.Bundle
import android.view.View
import android.webkit.WebView
import android.widget.TextView
import com.mechat.R
import com.mechat.webview.CustomWebView
import com.mechat.webview.JavaScriptMethod
import com.mechat.webview.WebViewClientListener
import com.custom.base.mvp.MVPBaseActivity
import kotlinx.android.synthetic.main.act_webview.*


/**
 * 作者：
 * 时间：2021-01-22
 * 描述：网页
 */

class WebViewActivity : MVPBaseActivity<WebViewContract.View, WebViewPresenter>(), WebViewContract.View {
    private var title = ""
    private var isShare = false //title右上角分享安宁
    private var isFull = false //全屏

    override fun initParms(parms: Bundle) {
        super.initParms(parms)
        title = parms.getString("title","")
        isShare = parms.getBoolean("share",false)
        isFull = parms.getBoolean("full",false)
    }

    override fun setFullScreen(): Boolean {
        return isFull
    }

    override fun bindLayout(): Int {
        return R.layout.act_webview
    }

    override fun initView() {
        webView.addJavascriptInterface(JavaScriptMethod(this, webView), "jsJavaScriptMethod")
    }

    override fun setTitleRightImageView(): List<Int> {
//        if(isShare) return listOf(R.drawable.icon_share_black)
        return listOf()
    }

    override fun onTitleRightClickListener(index: Int, view: View) {
        when (index) {
            0 -> {
                webView.loadJsFunction("javascript:invite()")
            }
        }
    }

    override fun getTitleTextView(tv: TextView) {
        if(!isFull){
            if(title != ""){
                tv.text = title
            }else{
                webView.setListenerWebViewClient(object : WebViewClientListener(){
                    override fun onPageFinished(view: WebView?, url: String?) {
                        view?.apply {
                            tv.text = this.title
                        }
                    }
                })
            }
        }
    }

    override val webView: CustomWebView
        get() = actWebView
}